package com.bcqn.interceptor;

import com.bcqn.model.Employee;
import com.bcqn.service.AdminService;
import com.bcqn.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class AdminInterceptor implements HandlerInterceptor {

    private AdminService adminService = new AdminService();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        //        String openId = getOpenId(request);
        //        return openId != null;
        System.out.println("测试暂时全部返回true");
        return true;
    }

    private String getEmployeeId(HttpServletRequest request) {
        Employee user = null;
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }
        try {
            for (Cookie cookie : cookies) {
                if ("_login_token".equals(cookie.getName())) {
                    String value = AESUtil.decode(cookie.getValue());
                    user = adminService.getUserByEmployeeId(Long.parseLong(value));
                    if (user == null) {
                        return null;
                    }
                    return value;
                }
            }
        } catch (Exception e) {
            log.error("cookie异常", e);
            return null;
        }
        return null;
    }
}
